package com.dream.dao.base;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.dream.model.app.AppCode;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.app.AppCodeVO;

@Repository
public class AppCodeDAO extends DreamBaseDAO<AppCode> {
	
	
	private DetachedCriteriaBuilder initQueryBuilder(final AppCodeVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppCode.class);
		builder.addEq("id", vo.getId());
		builder.addEq("deleted", false).addLikeAny("name", vo.getName());
		
		return builder;
	}

	public PagingVO query(final AppCodeVO vo, final PagingVO page){
		DetachedCriteriaBuilder queryBuilder = initQueryBuilder(vo);
		DetachedCriteriaBuilder countBuilder = initQueryBuilder(vo);
		return this.selectPagingVO(queryBuilder, page, countBuilder);
		
	}
	
	public boolean existed(final AppCodeVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppCode.class);
		builder.addEq("deleted", false).addEq("name", vo.getName());
		
		return CollectionUtils.isNotEmpty(select(builder));
		
	}
	
}
